<template>
	<view>
		<u-sticky>
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs>
		</u-sticky>
		<view class=" u-p-r-30 u-p-l-30 u-m-t-20">
			<u-row>
				<u-col span="11" text-align="center">
					<scan></scan>
					<scanner></scanner>
					<sm></sm>
					<honeywell></honeywell>
					<hk></hk>
					<u-search placeholder="请输入关键词搜索" :clearabled="true" v-model="keyword" border-color="#606266"
						@custom="search" @search="search"></u-search>
				</u-col>
				<u-col span="1" text-align="center" @click="scan()">
					<view class="u-m-t-14 weikaishi2">
						<u-icon name="scan" color="#2b85e4" size="43"></u-icon>
					</view>
				</u-col>
			</u-row>
		</view>
		<u-card v-if="current == 0">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="8" text-align="left">
							<view class=" weikaishi">
								<span>商品</span>
							</view>
						</u-col>
						<u-col span="4" text-align="right">
							<view class=" weikaishi">
								<span>托号</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-row v-for="(item,index) in dataList" :key="index" @click="receive(item)">
					<u-col span="8" text-align="left">
						<view class="lanse u-m-t-10 huanhang">
							<span>{{item.goodsName}}</span>
						</view>
					</u-col>
					<u-col span="4" text-align="right">
						<view class="u-m-t-10 huanhang">
							<span>{{item.trayNumber}}</span>
						</view>
					</u-col>
					<u-row>
						<u-col span="12" text-align="center">
							<view class="jiange">
							</view>
						</u-col>
					</u-row>
				</u-row>
			</view>
		</u-card>
		<u-card v-if="current == 1">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="6" text-align="left">
							<view class=" weikaishi">
								<span>商品</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class=" weikaishi">
								<span>托号</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class=" weikaishi">
								<span>领取人</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-swipe-action :show="item.show" :index="index" 
					v-for="(item, index) in swipelist" :key="index" 
					@click="swipeclick" @open="open"
					:options="options"
				>
					<u-row @click="toput(item.id,item.receiveUser,item.getWarehouseId)">
						<u-col span="6" text-align="left">
							<view class="lanse u-m-t-10 huanhang">
								<span>{{item.goodsName}}</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class="u-m-t-10 huanhang">
								<span>{{item.trayNumber}}</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class="u-m-t-10 huanhang">
								<span>{{item.receiveUserName}}</span>
							</view>
						</u-col>
						<u-col span="12" text-align="center">
							<view class="jiange">
							</view>
						</u-col>
					</u-row>
				</u-swipe-action>
			</view>
		</u-card>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	import scan from "@/components/uni-scan/scan.vue"
	import radio from "@/components/uni-scan/radio.js"
	import scanner from "@/components/uni-scan/scanner.vue"
	import sm from "@/components/uni-scan/sm.vue"
	import honeywell from "@/components/uni-scan/honeywell.vue"
    import { forEach } from "luch-request/src/lib/utils"
    import hk from "@/components/uni-scan/hk.vue"
	
	export default {
		components: {
			scan,
			scanner,
			sm,
			honeywell,
			hk
		},
		data() {
			return {
				flex: '',
				keyword: '',
				list: [{
						name: '待领取'
					},
					{
						name: '已领取'
					}
				],
				current: 0,
				dataList: [],
				dataList2:[],
				swipelist:[],
				pageNo: 1,
				hasMore: true,
				orderCount: 0,
				options: [
					{
						text: '撤销',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				],
				disabled: false,
				btnWidth: 180,
			}
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
		},
		onShow() {
			this.keyword=""
			this.refresh()
			let that = this
			uni.$off('scan')
			uni.$on('scan', function(res) {
				console.log('scan扫码结果：', res.codeStr);
				that.keyword = res.codeStr

				let youma = false //有扫描的商品吗
				let youitem = {}
				for (let i = 0; i < that.dataList2.length; i++) {
					let item = that.dataList2[i]
					if (that.keyword == item.trayNumber) {
						youma = true
						youitem = item
						break;
					}
				}
				if (youma) {
					uni.showModal({
						title: '确认领取吗？',
						content: '商品:' + youitem.goodsName + '，托号:' + youitem.trayNumber,
						success: (res) => {
							if (res.confirm) {
								that.$http.post('/rest/HxStoragePut/receive', {}, {
									params: {
										id: youitem.id
									},
									header: {
										'Authorization': uni.getStorageSync('token'),
									}
								}).then(res => {
									if (res.data.code == 200) {
										that.$refs.uToast.show({
											title: '领取成功',
											type: 'success'
										})
										that.keyword = "";
										that.$Router.push({
											name: 'hx_storage_put_task_put',
											params: {
												id: youitem.id,
												getWarehouseId:youitem.getWarehouseId
											},
										})
									} else {
										that.$refs.uToast.show({
											title: res.data.message,
											type: 'error'
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else {
					that.$refs.uToast.show({
						title: '该任务不存在',
						type: 'error'
					});
					console.log('scan_play_ding_fail')
					radio.play_ding_fail()
				}
			})
			uni.$off('scanner') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scanner', function(res) {
				console.log('scanner扫码结果：', res.code);
				that.keyword = res.code;

				let youma = false //有扫描的商品吗
				let youitem = {}
				for (let i = 0; i < that.dataList2.length; i++) {
					let item = that.dataList2[i]
					if (that.keyword == item.trayNumber) {
						youma = true
						youitem = item
						break;
					}
				}
				if (youma) {
					uni.showModal({
						title: '确认领取吗？',
						content: '商品:' + youitem.goodsName + '，托号:' + youitem.trayNumber,
						success: (res) => {
							if (res.confirm) {
								that.$http.post('/rest/HxStoragePut/receive', {}, {
									params: {
										id: youitem.id
									},
									header: {
										'Authorization': uni.getStorageSync('token'),
									}
								}).then(res => {
									if (res.data.code == 200) {
										that.$refs.uToast.show({
											title: '领取成功',
											type: 'success'
										})
										that.keyword = "";
										that.$Router.push({
											name: 'hx_storage_put_task_put',
											params: {
												id: youitem.id,
												getWarehouseId:youitem.getWarehouseId
												
											},
										})
									} else {
										that.$refs.uToast.show({
											title: res.data.message,
											type: 'error'
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else {
					that.$refs.uToast.show({
						title: '该任务不存在',
						type: 'error'
					});
					console.log('scan_play_ding_fail')
					radio.play_ding_fail()
				}
			})
			uni.$off('sm') // 每次进来先 移除全局自定义事件监听器
			uni.$on('sm', function(res) {
				console.log('sm扫码结果：', res.code);

				that.keyword = res.code;

				let youma = false //有扫描的商品吗
				let youitem = {}
				for (let i = 0; i < that.dataList2.length; i++) {
					let item = that.dataList2[i]
					if (that.keyword == item.trayNumber) {
						youma = true
						youitem = item
						break;
					}
				}
				if (youma) {
					uni.showModal({
						title: '确认领取吗？',
						content: '商品:' + youitem.goodsName + '，托号:' + youitem.trayNumber,
						success: (res) => {
							if (res.confirm) {
								that.$http.post('/rest/HxStoragePut/receive', {}, {
									params: {
										id: youitem.id
									},
									header: {
										'Authorization': uni.getStorageSync('token'),
									}
								}).then(res => {
									if (res.data.code == 200) {
										that.$refs.uToast.show({
											title: '领取成功',
											type: 'success'
										})
										that.keyword = "";
										that.$Router.push({
											name: 'hx_storage_put_task_put',
											params: {
												id: youitem.id,
												getWarehouseId:youitem.getWarehouseId
												
											},
										})
									} else {
										that.$refs.uToast.show({
											title: res.data.message,
											type: 'error'
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else {
					that.$refs.uToast.show({
						title: '该任务不存在',
						type: 'error'
					});
					console.log('scan_play_ding_fail')
					radio.play_ding_fail()
				}
			})
			uni.$off('honeywell') // 每次进来先 移除全局自定义事件监听器
			uni.$on('honeywell', function(res) {
				console.log('sm扫码结果：', res.code);
			
				that.keyword = res.code;
			
				let youma = false //有扫描的商品吗
				let youitem = {}
				for (let i = 0; i < that.dataList2.length; i++) {
					let item = that.dataList2[i]
					if (that.keyword == item.trayNumber) {
						youma = true
						youitem = item
						break;
					}
				}
				if (youma) {
					uni.showModal({
						title: '确认领取吗？',
						content: '商品:' + youitem.goodsName + '，托号:' + youitem.trayNumber,
						success: (res) => {
							if (res.confirm) {
								that.$http.post('/rest/HxStoragePut/receive', {}, {
									params: {
										id: youitem.id
									},
									header: {
										'Authorization': uni.getStorageSync('token'),
									}
								}).then(res => {
									if (res.data.code == 200) {
										that.$refs.uToast.show({
											title: '领取成功',
											type: 'success'
										})
										that.keyword = "";
										that.$Router.push({
											name: 'hx_storage_put_task_put',
											params: {
												id: youitem.id,
												getWarehouseId:youitem.getWarehouseId
												
											},
										})
									} else {
										that.$refs.uToast.show({
											title: res.data.message,
											type: 'error'
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else {
					that.$refs.uToast.show({
						title: '该任务不存在',
						type: 'error'
					});
					console.log('scan_play_ding_fail')
					radio.play_ding_fail()
				}
			})
			uni.$off('hk') // 每次进来先 移除全局自定义事件监听器
			uni.$on('hk', function(res) {
				console.log('hk扫码结果：', res.code);
			
				that.keyword = res.code;
			
				let youma = false //有扫描的商品吗
				let youitem = {}
				for (let i = 0; i < that.dataList2.length; i++) {
					let item = that.dataList2[i]
					if (that.keyword == item.trayNumber) {
						youma = true
						youitem = item
						break;
					}
				}
				if (youma) {
					uni.showModal({
						title: '确认领取吗？',
						content: '商品:' + youitem.goodsName + '，托号:' + youitem.trayNumber,
						success: (res) => {
							if (res.confirm) {
								that.$http.post('/rest/HxStoragePut/receive', {}, {
									params: {
										id: youitem.id
									},
									header: {
										'Authorization': uni.getStorageSync('token'),
									}
								}).then(res => {
									if (res.data.code == 200) {
										that.$refs.uToast.show({
											title: '领取成功',
											type: 'success'
										})
										that.keyword = "";
										that.$Router.push({
											name: 'hx_storage_put_task_put',
											params: {
												id: youitem.id,
												getWarehouseId:youitem.getWarehouseId
											},
										})
									} else {
										that.$refs.uToast.show({
											title: res.data.message,
											type: 'error'
										})
									}
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
				} else {
					that.$refs.uToast.show({
						title: '该任务不存在',
						type: 'error'
					});
					console.log('scan_play_ding_fail')
					radio.play_ding_fail()
				}
			})
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.current == 0) {
				uni.showLoading({
					title: '加载中'
				})
				if (this.hasMore) {
					this.pageNo++
					setTimeout(() => {
						this.loadOrder({
							condition: this.keyword
						}).then(res => {})
						uni.hideLoading()
					}, 500)
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}
		},
		methods: {
			swipeclick(index, index1) {
				uni.showModal({
					content: '确定撤销吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '加载中...'
							})
							this.$http.post('/rest/HxStoragePut/revokeReceive', {}, {
								params: {
									id: this.swipelist[index].id
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								uni.hideLoading()
								if (res.data.code == 200) {
									this.$refs.uToast.show({
										title: '撤销成功',
										type: 'success'
									})
									setTimeout(() => {
										this.refresh()
									}, 1000)
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
									setTimeout(() => {
										this.refresh()
									}, 1000)
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.swipelist[index].show = true;
				this.swipelist.map((val, idx) => {
					if(index != idx) this.swipelist[idx].show = false;
				})
			},
			scan() {
				let that = this
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.keyword = res.result

						let youma = false //有扫描的商品吗
						let youitem = {}
						for (let i = 0; i < that.dataList2.length; i++) {
							let item = that.dataList2[i]
							if (that.keyword == item.trayNumber) {
								youma = true
								youitem = item
								break;
							}
						}
						if (youma) {
							uni.showModal({
								title: '确认领取吗？',
								content: '商品:' + youitem.goodsName + '，托号:' + youitem.trayNumber,
								success: (res) => {
									if (res.confirm) {
										that.$http.post('/rest/HxStoragePut/receive', {}, {
											params: {
												id: youitem.id
											},
											header: {
												'Authorization': uni.getStorageSync(
													'token'),
											}
										}).then(res => {
											if (res.data.code == 200) {
												that.$refs.uToast.show({
													title: '领取成功',
													type: 'success'
												})
												that.keyword = "";
												that.$Router.push({
													name: 'hx_storage_put_task_put',
													params: {
														id: youitem.id
													},
												})
											} else {
												that.$refs.uToast.show({
													title: res.data.message,
													type: 'error'
												})
											}
										})
									} else if (res.cancel) {
										console.log('用户点击取消');
									}
								}
							})
						} else {
							that.$refs.uToast.show({
								title: '该任务不存在',
								type: 'error'
							});
							console.log('scan_play_ding_fail')
							radio.play_ding_fail()
						}

					}
				});
			},
			toput(id, receiveUser,getWarehouseId) {
				this.$http.post('/rest/hxStorageOffShelf/findUser2', {}, {
					params: {
						account: receiveUser
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					if (res.data.code == 200) {
						this.$Router.push({
							name: 'hx_storage_put_task_put',
							params: {
								id: id,
								getWarehouseId:getWarehouseId
							},
						})
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
					}
				})
			},
			receive(item) {
				uni.showModal({
					title: '确认领取吗？',
					content: '商品:' + item.goodsName + '，托号:' + item.trayNumber,
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/HxStoragePut/receive', {}, {
								params: {
									id: item.id
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								if (res.data.code == 200) {
									this.$refs.uToast.show({
										title: '领取成功',
										type: 'success'
									})
									this.$Router.push({
										name: 'hx_storage_put_task_put',
										params: {
											id: item.id,
											getWarehouseId:item.getWarehouseId
										},
									})
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			refresh() {
				if (this.current == 0) {
					this.pageNo = 1
					this.hasMore = true
					this.loadOrder({
						condition: this.keyword
					}).then(res => {})
					this.getall()
				} else if (this.current == 1) {
					this.$http.post('/rest/HxStoragePut/appfindlistyilingqu', {}, {
						params: {
							condition: this.keyword
						},
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						this.list[1].name = '已领取(' + res.data.data.length + ')'
						let newswipelist = []
						for (let item1 of res.data.data) {
							var arr = {}
							arr.id = item1.id
							arr.goodsName = item1.goodsName
							arr.trayNumber = item1.trayNumber
							arr.receiveUserName = item1.receiveUserName
							arr.receiveUser = item1.receiveUser
							arr.getWarehouseId=item1.getWarehouseId
							arr.show = false
							newswipelist.push(arr)
						}
						this.swipelist = newswipelist
					})
				}
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 15
					})
					this.$http.post('/rest/HxStoragePut/appfindPagedailingqu', {}, {
						params: param,
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						if (this.pageNo == 1) {
							this.dataList = res.data.data
						} else {
							this.dataList = this.dataList.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.list[0].name = '待领取(' + res.data.count + ')'
						this.hasMore = this.pageNo * 15 < res.data.count
						resolve(res)
					})
				})
			},
			getall(){
				this.$http.post('/rest/HxStoragePut/appfindPagedailingquall', {}, {
					params: {
						condition: this.keyword
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					this.dataList2 = res.data
				})
			},
			sectionChange(e) {
				this.current = e
				uni.showLoading({
					title: '加载中'
				})
				this.refresh()
				setTimeout(() => {
					uni.hideLoading()
				}, 500)
			},
			search(e) {
				uni.showLoading({
					title: '加载中'
				})
				this.refresh()
				setTimeout(() => {
					uni.hideLoading()
				}, 500)
			},
		}
	}
</script>


<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}

	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}

	.weikaishi2 {
		font-size: 34rpx;
	}

	.weikaishi3 {
		font-size: 30rpx;
	}

	.jiange {
		border-bottom: 1px solid #ebebeb;
	}
</style>
